#include "TextSceneNode.h"
#include "../include/PowerLibraryEngine.h"

CTextSceneNode::CTextSceneNode(IFont *fnt,stringc Text,AlignementX ax,AlignementY ay,vector2dvpos nSpace,vPos nPoints,
							   ISceneNodeReceiveParams):ITextSceneNode(ISceneNodeBasicParams),
							   TxtFont(fnt),Text(Text),Space(nSpace),Points(nPoints)
{
//	Space.X = 0.1;
	PositionAligned.Set(0.0,0.0,0.0);
	this->ax = ax;
	this->ay = ay;

	// Refractoriza
	IMaterial material;
//	Material = material;

	// Adding this texts to the list of texts created with a font.
	TxtFont->AddTextToThisFont(this);

	this->Text = "";
	SetText(Text);
}

CTextSceneNode::~CTextSceneNode()
{

}

void CTextSceneNode::Render(IDrawableFaceManager *dfm)
{
}

void CTextSceneNode::SetText(stringc newText)
{
	Text = newText;

	// First of all, we have to check if we have less children as the legth of the text.
	while (Text.size() > VectorLetter.size())
	{
		s8 Char = Text[VectorLetter.size()];
		CLetterSceneNode *LetterNode = GetNewLetter(Char,TxtFont);
		VectorLetter.push_back(LetterNode);
	}

	ReUpdate();
}

void CTextSceneNode::ReUpdate()
{
	TextSize.set(0.0,0.0);
	for (u32 i=0;i<Text.size();i++)
	{
		TextSize.X += VectorLetter[i]->GetLenX();
		if (TextSize.Y < VectorLetter[i]->GetLenY())
			TextSize.Y = VectorLetter[i]->GetLenY();
	}

	for (u32 i=0;i<Text.size();i++)
	{
		TextSize.X += Space.X;
	}

	vector2dvpos Pos(0.0,0.0);

	switch (ax)
	{
		default:
		case ALIGNEMENT_X_LEFT:
		break;

		case ALIGNEMENT_X_CENTER:
			Pos.X = (vPos)(-(TextSize.X / 2.0));
		break;

		case ALIGNEMENT_X_RIGHT:
			Pos.X -= TextSize.X;
		break;
	}

	for (u32 i=0;i<Text.size();i++)
	{
		Pos.X += (vPos)((VectorLetter[i]->GetLenX() / 2.0) + (Space.X / 2.0));
		VectorLetter[i]->SetPositionX((vPos)Pos.X);
		VectorLetter[i]->SetPositionY((vPos)Pos.Y);
		if (i<(Text.size()-1))
			Pos.X += (vPos)((VectorLetter[i]->GetLenX() / 2.0) + (Space.X / 2.0));
	}
}

void CTextSceneNode::SetPositionAligned(vector3dvpos &nv)
{
	if (nv.X != PositionAligned.X)
		SetPositionAlignedX(nv.X);

	if (nv.Y != PositionAligned.Y)
		SetPositionAlignedY(nv.Y);

	if (nv.Z != PositionAligned.Z)
		SetPositionAlignedX(nv.Z);
}

void CTextSceneNode::SetPositionAlignedX(vPos nv)
{
	vPos PosX(nv);

	switch (ax)
	{
		default:
		case ALIGNEMENT_X_LEFT:
			PosX += (vPos)(TextSize.X / 2.0);
			PosX += (vPos)(VectorLetter[0]->GetLenX());
		break;

		case ALIGNEMENT_X_CENTER:
		break;

		case ALIGNEMENT_X_RIGHT:
			PosX -= (vPos)(TextSize.X / 2.0);
			PosX -= (vPos)(VectorLetter[VectorLetter.size()-1]->GetLenX());
		break;
	}

	SetPositionX(PosX);
}

void CTextSceneNode::SetPositionAlignedY(vPos nv)
{
	vPos PosY(nv);

	switch (ay)
	{
		default:
		case ALIGNEMENT_Y_UNDER:
			PosY -= (vPos)(TextSize.Y / 2.0);
		break;

	case ALIGNEMENT_Y_MIDLE:
		break;

	case ALIGNEMENT_Y_OVER:
			PosY += (vPos)(TextSize.Y / 2.0);
		break;
	}

	SetPositionY(PosY);
}

void CTextSceneNode::SetSpace(vector2dvpos &nv)
{
	SetSpaceX(nv.X);
	SetSpaceY(nv.Y);
}

void CTextSceneNode::SetSpaceX(vPos nv)
{
	if (nv != Space.X)
	{
		Space.X = nv;
		ReUpdate();
	}
}

void CTextSceneNode::SetSpaceY(vPos nv)
{
	if (nv != Space.Y)
	{
		Space.Y = nv;
		ReUpdate();
	}
}

void CTextSceneNode::SetPositionAlignedZ(vPos nv)
{
}

void CTextSceneNode::SetAlignementX(AlignementX ax)
{
	this->ax = ax;
}

void CTextSceneNode::SetAlignementY(AlignementY ay)
{
	this->ay = ay;
}

const stringc &CTextSceneNode::GetText()
{
	return Text;
}

CLetterSceneNode *CTextSceneNode::GetNewLetter(s8 Letter,IFont *Font)
{
	CLetterSceneNode *NewLetter = new CLetterSceneNode(Font,Letter,Points);
	NewLetter->SetParent(this);
	return NewLetter;
}

void CTextSceneNode::SetForeColor(CColor &color)
{
	SetMaterialAmbients(color);
}

void CTextSceneNode::SetBackColor(CColor &color)
{

}

void CTextSceneNode::SetMaterialAmbients(CColor &Color)
{
	for (VectorLetterType::iterator it=VectorLetter.begin();it!=VectorLetter.end();it++)
	{
		(*it)->GetDrawableFace()->SetAmbient(Color);
	}
}
